package View;


import database.ConnectData;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import redis.clients.jedis.Jedis;

public class SignMenu {
    private String username;
    private String password1;
    private String text;
    private int flag = 0;
    public JTextField id;
    private String name;
    private String select;

    public void init() {

        JFrame f = new JFrame("欢迎使用租书店管理系统");

        //设置窗口大小
        f.setSize(600, 600);

        //使窗口居中
        f.setLocationRelativeTo(null);

        //设置布局为空
        f.setLayout(null);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
        JPanel panel5 = new JPanel();
        JPanel panel6 = new JPanel();

        /*标题*/
        JLabel lable = new JLabel("租书店管理系统");
        Font font = new Font("宋体",Font.BOLD,40);//设置字体
        lable.setFont(font);
        panel1.add(lable);
        panel1.setBounds(140, 60, 300, 300);//设置面板大小
        f.add(panel1);

        /*用户类型标签*/
        JLabel user = new JLabel("用户类型  ");
        Font font1 = new Font("宋体",Font.BOLD,25);
        user.setFont(font1);
        panel2.add(user);

        /*用户类型的下拉框*/
        JComboBox<String> j = new JComboBox<String>();
        Dimension dimension = new Dimension(200,30);//框框的大小
        j.setPreferredSize(dimension);
        j.addItem("顾客");
        j.addItem("管理员");
        Font font2 = new Font("宋体",Font.BOLD,15);
        j.setFont(font2);
        j.setBackground(new Color(51,153,255));
        panel2.setBounds(90, 160, 400, 300);
        panel2.add(j);
        f.add(panel2);

        /*账号标签*/
        JLabel labid = new JLabel("   账号： ");
        Font font3 = new Font("宋体",Font.BOLD,25);
        labid.setFont(font3);
        panel3.add(labid);

        /*输入账户的文本框*/
        id = new JTextField();
        id.setPreferredSize(dimension);
        panel3.add(id);
        panel3.setBounds(90, 230, 400, 300);
        f.add(panel3);

        /*密码标签*/
        JLabel labpassword = new JLabel("   密码： ");
        Font font4 = new Font("宋体",Font.BOLD,25);
        labpassword.setFont(font4);
        panel4.add(labpassword);

        /*输入密码文本框*/
        JPasswordField password = new JPasswordField();
        password.setPreferredSize(dimension);
        Font font5 = new Font("宋体",Font.BOLD,25);
        password.setFont(font5);
        password.setBackground(new Color(51,153,255));
        panel4.add(password);
        panel4.setBounds(90, 290, 400, 300);
        f.add(panel4);

        /*注册登陆按钮*/
        JButton button1 = new JButton("注册");
        JButton button2 = new JButton("登陆");
        button1.setFont(font4);
        button2.setFont(font4);
        Dimension dimension2 = new Dimension(100,50);
        button1.setPreferredSize(dimension2);
        button2.setPreferredSize(dimension2);
        button1.setBackground(new Color(51,153,255));
        button2.setBackground(new Color(51,153,255));
        panel5.add(button1);
        panel6.add(button2);
        panel5.setBounds(150, 370, 150, 400);
        panel6.setBounds(330, 370, 150, 400);
        f.add(panel5);
        f.add(panel6);

        id.setOpaque(false);
        password.setOpaque(false);
        panel1.setOpaque(false);
        panel2.setOpaque(false);
        panel3.setOpaque(false);
        panel4.setOpaque(false);
        panel5.setOpaque(false);
        panel6.setOpaque(false);


        /*用户类型*/
        select = "顾客";
        j.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED) {
                    select = (String) e.getItem();
                }

            }
        });



        /*点击注册跳转到注册界面*/
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                f.dispose();
                Register r = new Register();
                try {
                    r.show();
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        /*登陆信息比对*/
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                text = j.getToolTipText();
                username = id.getText().trim();
                password1 = password.getText().trim();

                try {
                    flag = select(username,password1);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                if(flag == 1) {//密码输入正确
                    name = id.getText().trim();

                    if(select.equals("顾客")) {
                        f.dispose();
                        new User(name);
                    }else if(select.equals("管理员")) {
                        flag = 0;
                        try {
                            flag = ensure(name);
                        } catch (Exception e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        if(flag == 1) {
                            new Admin(name);
                            f.dispose();
                        }else {
                            JOptionPane.showMessageDialog(null, "对不起，您还不是管理员,请选择顾客登陆", "警告", JOptionPane.WARNING_MESSAGE);
                        }
                    }
                }else if(flag == 0) {//输入错误
                    JOptionPane.showMessageDialog(null, "信息输入有误,请检查", "抱歉", JOptionPane.PLAIN_MESSAGE);
                }
            }
        });

        //设置不可改变窗口大小
        f.setResizable(false);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public int select(String username1,String password1) throws Exception {
        boolean flag = false;

        Jedis jedis = new Jedis("127.0.0.1",6379);
        String s1 = username;
        String s2 = password1;

        if(s2.equals(jedis.hget(s1, "pw"))){    //都对了
           return  1;
        }
        else{
            return 0;                               //不是本人
        }

    }
    public int ensure(String username) throws Exception{
        String sql = "select * from user where username=?";
        Connection con;
        PreparedStatement ps ;
        ResultSet rs;
        ConnectData cd = new ConnectData();
        con = cd.connect();
        ps = con.prepareStatement(sql);
        ps.setString(1, username) ;
        rs = ps.executeQuery();
        int s1=0;
        while(rs.next()) {
            s1 = rs.getInt(4);
        }
        return s1;
    }
}
